package com.cnyz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.cnyz.base.PageResponse;
import com.cnyz.dto.LoginDto;
import com.cnyz.entity.User;
import com.cnyz.dto.UserDto;
import com.cnyz.exception.BaseException;
import com.cnyz.mapper.CustomerMapper;
import com.cnyz.properties.JwtTokenManagerProperties;
import com.cnyz.service.CustomerService;
import com.cnyz.utils.JwtUtil;
import com.cnyz.vo.LoginVo;
import com.cnyz.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.prefs.BackingStoreException;

import static com.cnyz.constant.Constants.JWT_USERID;
import static com.cnyz.constant.Constants.JWT_USERNAME;

@Service
public class CustomerServiceImp implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;

    @Override
    public PageResponse<UserVo> getUserPage(Integer pageNum,Integer pageSize,Integer role,String username) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVo> users = customerMapper.getUserPage(username,role);
        Page<UserVo> pages = (Page<UserVo>) users;
        return PageResponse.of(pages, UserVo.class);
    } // 分页查询用户信息

    @Override
    public Long insertCustomer(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        System.out.println("数据传入" + userDto);
        customerMapper.insertCustomer(user);
        return user.getId();
    }//用户注册

    @Override
    public void deleteUserById(Long id) {
        if (id == null) {
            throw new BaseException("用户ID不能为空");
        }
        customerMapper.deleteById(id);
    }

    @Override
    public LoginVo login(LoginDto loginDto) {
        String email = loginDto.getEmail();
        String password = loginDto.getPassword();
        User user = customerMapper.getByEmail(email);
        System.out.println("搜索到User"+user);
        //没有这个用户
        if (user == null) {
            throw new BaseException("用户不存在");
        }

        //密码不匹配
        if (!ObjectUtil.equals(user.getPassword(), password)) {
            throw new BaseException("密码错误");
        }

        //创建JWT令牌
        LoginVo loginVo = new LoginVo();
        HashMap <String,Object>claims = new HashMap();
        claims.put(JWT_USERID,user.getId());
        claims.put(JWT_USERNAME,user.getUsername());
        claims.put("role", user.getRole()); // 添加角色信息到JWT中
        String token = JwtUtil.createJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(), jwtTokenManagerProperties.getTtl(),claims);
        loginVo.setToken(token);
        loginVo.setUsername(user.getUsername());
        loginVo.setEmail(user.getEmail());
        loginVo.setId(user.getId());
        return loginVo;

    }// 用户登录，返回LoginVo包含JWT等信息

    @Override
    public void resetPassword(UserDto userDto) {
        String email = userDto.getEmail();
        User user = customerMapper.getByEmail(email);
        if (user == null) {
            throw new BaseException("用户不存在");
        }
        if( !ObjectUtil.equals(user.getPassword(), userDto.getPassword())) {
            throw new BaseException("密码错误");
        }

        if(ObjectUtil.equals(user.getPassword(), userDto.getNewPassword())){
            throw new BaseException("新密码不能与旧密码相同");
        }

        customerMapper.updatePassword(userDto.getNewPassword());
    } // 重置密码

    @Override
    public UserVo getUserByEmail(String email) {
        User user = customerMapper.getByEmail(email);
        if (user == null) {
            return null;
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    } // 根据邮箱获取用户信息

    @Override
    public UserVo getUserById(Long id) {
        User user = customerMapper.getById(id);
        if (user == null) {
            return null;
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }// 根据ID获取用户信息


}
